package com.framework.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
// Order 要设置有间隔，防止中间又有新的切面加入，导致需要修改3个切面。10.20.30
@Order(value = 10)
public class AroundAspects {

    @Pointcut("execution(* com.framework.service.impl.*.add(..))")
    public void service_add() {
    }

    @Around("service_add()")
    public Object object(ProceedingJoinPoint joinPoint) {

        // 前置通知
        System.out.println("环绕-前置通知："+"目标对象："+joinPoint.getTarget().getClass().getName()
        +"，切面方法名称："+joinPoint.getSignature().getName()+"，参数信息"+ Arrays.toString(joinPoint.getArgs()));
        Object[] args = joinPoint.getArgs();
        Object proceed;
        try {
            proceed = joinPoint.proceed(args);
            // 后置返回
            System.out.println("环绕-后置返回：" + proceed);
        } catch (Throwable e) {
            // 后置异常
            System.out.println("环绕-后置异常：" + e.getMessage());
            throw new RuntimeException(e);
        } finally {
            // 后置通知
            System.out.println("环绕-后置通知：结束");
        }
        return proceed;
    }

}
